package com.ruoyi.source.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.db.domain.ChannelInfo;
import com.ruoyi.db.mapper.ChannelInfoMapper;
import com.ruoyi.market.constant.RedisKeyConstent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.SourceBaseInfoMapper;
import com.ruoyi.db.domain.SourceBaseInfo;
import com.ruoyi.source.service.ISourceBaseInfoService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 来源基本Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-22
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class SourceBaseInfoServiceImpl implements ISourceBaseInfoService 
{
    @Autowired
    private SourceBaseInfoMapper sourceBaseInfoMapper;
    @Resource
    private ChannelInfoMapper channelInfoMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询来源基本
     * 
     * @param id 来源基本主键
     * @return 来源基本
     */
    @Override
    public SourceBaseInfo selectSourceBaseInfoById(Long id)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoById(id);
    }

    /**
     * 查询来源基本列表
     * 
     * @param sourceBaseInfo 来源基本
     * @return 来源基本
     */
    @Override
    public List<SourceBaseInfo> selectSourceBaseInfoList(SourceBaseInfo sourceBaseInfo)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoList(sourceBaseInfo);
    }

    /**
     * 查询来源基本列表
     *
     * @param sourceBaseInfo 来源基本
     * @return 来源基本
     */
    @Override
    public List<Map<String, Object>> selectSourceBaseInfoListMap(SourceBaseInfo sourceBaseInfo)
    {
        return sourceBaseInfoMapper.selectSourceBaseInfoListMap(sourceBaseInfo);
    }

    /**
     * 新增来源基本
     * 
     * @param sourceBaseInfo 来源基本
     * @return 结果
     */
    @Override
    public int insertSourceBaseInfo(SourceBaseInfo sourceBaseInfo)
    {

        // 根据渠道编号，查询渠道信息
        ChannelInfo channelInfo = channelInfoMapper.selectChannelInfoByChannelCode(sourceBaseInfo.getChannelCode());
        if (ObjectUtil.isEmpty(channelInfo)){
            return -1;
        }

        sourceBaseInfo.setChannelName(channelInfo.getChannelName());

        sourceBaseInfo.setCreateTime(DateUtils.getNowDate());
        return sourceBaseInfoMapper.insertSourceBaseInfo(sourceBaseInfo);
    }

    /**
     * 修改来源基本
     * 
     * @param sourceBaseInfo 来源基本
     * @return 结果
     */
    @Override
    public int updateSourceBaseInfo(SourceBaseInfo sourceBaseInfo)
    {
        String redisKey = StrUtil.format(RedisKeyConstent.SOURCEIFNO_CODE_KEY, sourceBaseInfo.getChannelCode());
        redisTemplate.delete(redisKey);
        sourceBaseInfo.setUpdateTime(DateUtils.getNowDate());
        return sourceBaseInfoMapper.updateSourceBaseInfo(sourceBaseInfo);
    }

    /**
     * 批量删除来源基本
     * 
     * @param ids 需要删除的来源基本主键
     * @return 结果
     */
    @Override
    public int deleteSourceBaseInfoByIds(String ids)
    {
        return sourceBaseInfoMapper.deleteSourceBaseInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除来源基本信息
     * 
     * @param id 来源基本主键
     * @return 结果
     */
    @Override
    public int deleteSourceBaseInfoById(Long id)
    {
        return sourceBaseInfoMapper.deleteSourceBaseInfoById(id);
    }
}
